package com.v1.video.util;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.v1.video.R;
import com.v1.video.activity.CibnVideoDetailActivity;
import com.v1.video.domain.CibnVideoEntity.Anthologys;
import com.v1.video.view.CustomShareSheetDialog;

public class PopupWindowUtils {

	public interface OnActionSheetItemClick {
		public void onClick(int index, String actionName);
	}

	private static PopupWindowUtils mInstance;

	private PopupWindowUtils() {

	}

	public static PopupWindowUtils getInstance() {
		if (mInstance == null) {
			mInstance = new PopupWindowUtils();
		}
		return mInstance;
	}

	public PopupWindow showPopupWindow(Context context, View parent,
			int height, final List<Anthologys> lst,String genre,final int curIndex,
			final OnActionSheetItemClick onActionSheetItemClick) {
		View view = View.inflate(context, R.layout.popupwindow_anthology, null);
		final PopupWindow popup = new PopupWindow(view,
				LinearLayout.LayoutParams.MATCH_PARENT, height);
		GridView gridview = (GridView) view.findViewById(R.id.gv_gridview);
		if(CibnVideoDetailActivity.CIBN_VARIETY.equals(genre)){
			gridview.setNumColumns(2);
		} else {
			gridview.setNumColumns(5);
		}
		gridview.setAdapter(new MyAdapter(context, lst, genre, curIndex,onActionSheetItemClick));
		view.findViewById(R.id.iv_close).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
					}
				});
		popup.setAnimationStyle(android.R.style.Animation);
		popup.setBackgroundDrawable(new BitmapDrawable());
		popup.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		
		return popup;
	}
	
	public PopupWindow sharePopupWindow(Context context, View parent,
			int height, final OnActionSheetItemClick onActionSheetItemClick) {
		View view = View.inflate(context, R.layout.popupwindow_share, null);
		final PopupWindow popup = new PopupWindow(view,
				LinearLayout.LayoutParams.MATCH_PARENT, height);
		((TextView)view.findViewById(R.id.tv_title)).setText("分享");
		view.findViewById(R.id.lay_share_weibo).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_SINA_WEIBO, "微博");
						}
					}
				});
		
		view.findViewById(R.id.lay_share_qq).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_QQ, "QQ");
						}
					}
				});
		
		view.findViewById(R.id.lay_share_weixin).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_WEIXIN, "微信");
						}
					}
				});
		
		view.findViewById(R.id.lay_share_qzone).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_QQ_SPACE, "QQ空间");
						}
					}
				});
		
		view.findViewById(R.id.lay_share_moments).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_WEIXIN_MOMENTS, "朋友圈");
						}
					}
				});
		
		view.findViewById(R.id.lay_share_link).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
						if(onActionSheetItemClick!=null){
							onActionSheetItemClick.onClick(CustomShareSheetDialog.TYPE_COPY, "复制链接");
						}
					}
				});

		view.findViewById(R.id.iv_close).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (popup != null && popup.isShowing()) {
							popup.dismiss();
						}
					}
				});
		popup.setAnimationStyle(android.R.style.Animation);
		popup.setBackgroundDrawable(new BitmapDrawable());
		popup.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

		return popup;
	}

	private class MyAdapter extends BaseAdapter {
		private Context mContext;
		private List<Anthologys> mLstData;
		private String mGenre;
		private int mCurIndex = 0;
		private OnActionSheetItemClick mOnActionSheetItemClick;

		public MyAdapter(Context context, List<Anthologys> lst, String genre,
				int curIndex, OnActionSheetItemClick onActionSheetItemClick) {
			mContext = context;
			mLstData = lst;
			mGenre = genre;
			mCurIndex = curIndex;
			mOnActionSheetItemClick = onActionSheetItemClick;
		}

		public void setCurIndex(int curIndex) {
			this.mCurIndex = curIndex;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mLstData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mLstData.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder holder = null;
			if (convertView == null) {
				convertView = View.inflate(mContext,
						R.layout.popupwindow_anthology_item, null);
				holder = new Holder();
				holder.name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			final Anthologys info = mLstData.get(position);
			if (CibnVideoDetailActivity.CIBN_VARIETY.equals(mGenre)) {
				if (!TextUtils.isEmpty(info.getTitle())) {
					holder.name.setText(info.getTitle());
				} else {
					holder.name.setText("");
				}
			} else {
				if (!TextUtils.isEmpty(info.getSort())) {
					holder.name.setText(info.getSort());
				} else {
					holder.name.setText("");
				}
			}
			if (position == mCurIndex) {
				holder.image.setVisibility(View.VISIBLE);
				holder.name.setVisibility(View.GONE);
			} else {
				holder.image.setVisibility(View.GONE);
				holder.name.setVisibility(View.VISIBLE);
			}
			final int index = position;
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (mOnActionSheetItemClick != null && mCurIndex != index) {
						mOnActionSheetItemClick.onClick(index, info.getTitle());
						mCurIndex = index;
						MyAdapter.this.notifyDataSetChanged();
					}
				}
			});
			return convertView;
		}

		private class Holder {
			private ImageView image;
			private TextView name;
		}

	}

}
